#include <cmath>
#include <iostream>
#include <vector>
#include <queue>
#include <algorithm>
#include <map>
#include <set>
#include <cstring>
#define endl "\n"
using namespace std;
int a[1010][110];
int b[100][3];
int f(int t, int m)
{
	if (a[t][m] != 0)
	{
		return a[t][m];
	}
	if (m == 0||t==0)
		return 0;
	if (t < b[m][1])
		return a[t][m]= f(t, m - 1);
	else
		return a[t][m]= max(f(t, m - 1), f(t - b[m][1], m - 1) + b[m][2]);
}
int main()
{
	int t, m;
	cin >> t >> m;
	for (int i = 1; i <= m; i++)
	{
		cin >> b[i][1] >> b[i][2];
	}
	cout << f(t, m) << endl;
	return 0;
}